﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using WebTV_Kickstarter;

namespace Kickstarter.Controllers
{
    public class GoalController : Controller
    {
        private EFDataModelContainer db = new EFDataModelContainer();

        // GET: /Goal/
        public ActionResult Index()
        {
            var goalset = db.GoalSet.Include(g => g.Project);
            return View(goalset.ToList());
        }

        // GET: /Goal/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Goal goal = db.GoalSet.Find(id);
            if (goal == null)
            {
                return HttpNotFound();
            }
            return View(goal);
        }

        // GET: /Goal/Create
        public ActionResult Create(int? id)
        {
            Goal goal = new Goal();
            goal.Project = Project.FindProject(id);
            goal.ProjectId = goal.Project.Id;
            return View(goal);
        }

        // POST: /Goal/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="Id,Title,Amount,Description,ProjectId")] Goal goal)
        {
            if (ModelState.IsValid)
            {
                db.GoalSet.Add(goal);
                db.SaveChanges();
                if (Session["redirectBack"] != null)
                {
                    RedirectToRouteResult r = RedirectToAction(Session["redirectBack"].ToString());
                    Session.Remove("redirectBack");
                    return r;
                }
                else
                    return RedirectToAction("Index");
            }

            ViewBag.ProjectId = new SelectList(db.ProjectSet, "Id", "Title", goal.ProjectId);
            return View(goal);
        }

        // GET: /Goal/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Goal goal = db.GoalSet.Find(id);
            if (goal == null)
            {
                return HttpNotFound();
            }
            ViewBag.ProjectId = new SelectList(db.ProjectSet, "Id", "Title", goal.ProjectId);
            return View(goal);
        }

        // POST: /Goal/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="Id,Title,Amount,Description,ProjectId")] Goal goal)
        {
            if (ModelState.IsValid)
            {
                db.Entry(goal).State = EntityState.Modified;
                db.SaveChanges();
                if (Session["redirectBack"] != null)
                {
                    RedirectToRouteResult r = RedirectToAction(Session["redirectBack"].ToString());
                    Session.Remove("redirectBack");
                    return r;
                }
                else
                    return RedirectToAction("Index");
            }
            ViewBag.ProjectId = new SelectList(db.ProjectSet, "Id", "Title", goal.ProjectId);

            return View(goal);
        }

        // GET: /Goal/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Goal goal = db.GoalSet.Find(id);
            if (goal == null)
            {
                return HttpNotFound();
            }
            return View(goal);
        }

        // POST: /Goal/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Goal goal = db.GoalSet.Find(id);
            db.GoalSet.Remove(goal);
            db.SaveChanges();
            if (Session["redirectBack"] != null)
            {
                RedirectToRouteResult r = RedirectToAction(Session["redirectBack"].ToString());
                Session.Remove("redirectBack");
                return r;
            }
            else
                return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
